﻿using SqlSugar.IOC;
using SqlSugar;
using SqlSugarRedisDemo.Models;

namespace SqlSugarRedisDemo.Services
{
    public class UserService
    {
        protected SqlSugarScopeProvider mSqlSugarProvider => DbScoped.SugarScope.GetConnectionScope("BridgeMonitor");

        public List<UserInfo> GetUserInfos(string? name = "") 
        {
            return mSqlSugarProvider.Queryable<UserInfo>()
                .WhereIF(!string.IsNullOrWhiteSpace(name), w => w.UserName.Contains(name))
                .WithCache("AllUserInfoKey")
                .ToList();
        }

        public UserInfo GetUserById(long id)
        {
            return mSqlSugarProvider.Queryable<UserInfo>()
                .Where(w => w.ID == id)
                .WithCache("UserInfoKey_" + id.ToString())
                .First();
        }

        public bool AddUser(UserInfo userInfo)
        {
            return mSqlSugarProvider.Insertable<UserInfo>(userInfo)
                .RemoveDataCache("*UserInfo*")
                .ExecuteCommand() > 0;
        }
        public bool UpdUser(UserInfo userInfo)
        {
            return mSqlSugarProvider.Updateable<UserInfo>(userInfo)
                .RemoveDataCache()
                .ExecuteCommand() > 0;
        }

        public bool DelUser(long id)
        {
            return mSqlSugarProvider.Deleteable<UserInfo>(id)
                .RemoveDataCache()
                .ExecuteCommand() > 0;
        }
    }
}
